package com.xiuxia.usercenterbackend.controller;

import com.xiuxia.usercenterbackend.common.R;
import com.xiuxia.usercenterbackend.common.ResponseCode;
import com.xiuxia.usercenterbackend.common.exception.BusinessException;
import com.xiuxia.usercenterbackend.domain.User;
import com.xiuxia.usercenterbackend.domain.request.UserLoginRequest;
import com.xiuxia.usercenterbackend.domain.request.UserRegisterRequest;
import com.xiuxia.usercenterbackend.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName UserController
 * @Author CHENBO
 * @Date 2024/11/27 14:36
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户注册
     *
     * @param userRegisterRequest 用户注册请求体
     * @return 用户id
     */
    @PostMapping("/register")
    public R<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
           throw new BusinessException(ResponseCode.PARAM_ERROR);
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
            throw new BusinessException(ResponseCode.PARAM_ERROR);
        }
        return R.ok(userService.userRegister(userAccount, userPassword, checkPassword));
    }

    /**
     * 获取用户登录态
     *
     * @param request http请求
     * @return 用户登录信息
     */
    @GetMapping("/current")
    public R<User> getUserCurrent(HttpServletRequest request) {
        return R.ok(userService.getUserCurrent(request));
    }

    /**
     * 用户登录
     *
     * @param userLoginRequest 用户登录请求体
     * @param request          请求
     * @return 用户
     */
    @PostMapping("/login")
    public R<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            throw new BusinessException(ResponseCode.PARAM_ERROR);
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            throw new BusinessException(ResponseCode.PARAM_ERROR);
        }
        return R.ok(userService.userLogin(userAccount, userPassword, request));
    }

    @PostMapping("/logout")
    public R<Integer> userLogout(HttpServletRequest request) {
        if (request == null) {
            return null;
        }
        return R.ok(userService.userLogout(request));
    }

    /**
     * 查询用户
     *
     * @param nickName 前端传入的用户昵称
     * @param request  请求
     * @return 符合条件的用户列表
     */

    @GetMapping("/search")
    public R<List<User>> searchUsers(String nickName, HttpServletRequest request) {
        return R.ok(userService.searchUsers(nickName, request));
    }

    /**
     * 删除用户
     *
     * @param id      用户id
     * @param request 请求
     * @return 删除是否成功
     */
    @DeleteMapping("/deleted/{id}")
    public R<Boolean> deletedUser(@PathVariable Long id, HttpServletRequest request) {
        return R.ok(userService.deletedUser(id, request));
    }
}
